package com.hliushi.webui.controller;

import com.hliushi.webui.bean.Result;
import com.hliushi.webui.mapper.CovidMapper;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName Covid19Controller
 * @Description 用来接收前端数据请求的Controller
 * @Author hliushi
 * @Date 2021/7/2 19:49
 * @Version V1.0
 **/
// 该类是Springboot的一个Controller, 返回的数据为JSON格式
@RestController //  == > @Controller + @ResponseBody
@RequestMapping("/covid")
public class Covid19Controller {

    private static final Logger LOGGER = LoggerFactory.getLogger(Covid19Controller.class);
    @Autowired
    private CovidMapper covidMapper;

    /**
     * 接收前端请求返回全国疫情汇总数据
     *
     * @return result
     */
    @RequestMapping("/getNationalData")
    public Result getNationalData() {

        LOGGER.info("====== 接收到前端发起的获取JSON数据的请求, 后续需要查询mysql将数据返回 ======");

        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        Map<String, Object> data = covidMapper.getNationalData(datetime).get(0);

        if (data != null) {
            return Result.success(data);
        } else {
            return Result.fail();
        }
    }

    /**
     * 查询全国各省份累计确诊数据并返回
     *
     * @return result
     */
    @RequestMapping("/getNationalMapData")
    public Result getNationalMapData() {

        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        List<Map<String, Object>> data = covidMapper.getNationalMapData(datetime);
        if (data != null) {
            return Result.success(data);
        } else {
            return Result.fail();
        }
    }


    /**
     * 查询全国每一天的疫情数据并返回
     *
     * @return result
     */
    @RequestMapping("/getCovidTimeData")
    public Result getCovidTimeData() {
        List<Map<String, Object>> data = covidMapper.getCovidTimeData();

        if (data != null) {
            return Result.success(data);
        } else {
            return Result.fail();
        }
    }


    /**
     * 查询各省份境外输入病例数量
     *
     * @return result
     */
    @RequestMapping("/getCovidImportData")
    public Result getCovidImportData() {

        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        List<Map<String, Object>> data = covidMapper.getCovidImportData(datetime);

        if (data != null) {
            return Result.success(data);
        } else {
            return Result.fail();
        }
    }


    /**
     * 查询各物资使用情况
     *
     * @return result
     */
    @RequestMapping("/getCovidWz")
    public Result getCovidWz() {
        List<Map<String, Object>> data = covidMapper.getCovidWz();

        if (data != null) {
            return Result.success(data);
        } else {
            return Result.fail();
        }
    }


}
